package com.cy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cy.common.entity.Result;
import com.cy.pojo.Campus;
import com.cy.pojo.vo.SearchVo;
import com.cy.service.CampusService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @description 驾校表
 * @author 残影
 * @date 2023-04-10
 */
@RestController
@RequestMapping("/campus")
@Api(tags = "驾校表模块")
public class CampusController {
    @Autowired
    private CampusService campusService;

    /**
     * 根据ID查询驾校表
     */
    @GetMapping("{id}")
    @ApiOperation("根据ID查询驾校表")
    public Result findById(@PathVariable Long id) {
        Campus campus = campusService.getById(id);
        if (campus != null) {
            return Result.ok("查询驾校表成功", campus);
        } else {
            return Result.failed(201, "没有该驾校表");
        }
    }

    /**
    * 分页查询驾校表
    */
    @ApiOperation("分页查询驾校表")
    @GetMapping("/list")
    public Result list(SearchVo searchVo) {
        //分页构造器
        Page<Campus> buildPage = new Page<>(searchVo.getPage(), searchVo.getLimit());
        //条件构造器
        QueryWrapper<Campus> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(searchVo.getName())) {
            queryWrapper.like("name", searchVo.getName());
        }
//        Date startTime = searchVo.getStartTime();
//        Date endTime = searchVo.getEndTime();
//        if (ObjectUtils.isNotEmpty(startTime) && ObjectUtils.isNotEmpty(endTime)) {
//            queryWrapper.between("create_time", startTime, endTime);
//        }
        IPage<Campus> pageList = campusService.page(buildPage, queryWrapper);
        return Result.ok("查询成功", pageList.getRecords()).put("total", pageList.getTotal());
    }

    /**
     * 保存驾校表
     */
    @PostMapping
    @ApiOperation("保存驾校表")
    public Result save(@RequestBody Campus campus) {
        campus.setCreateTime(new Date());
        campus.setUpdateTime(new Date());
        boolean b = campusService.save(campus);
        if (b) {
            return Result.ok("保存用户驾校表成功", campus);
        } else {
            return Result.failed("保存驾校表失败");
        }
    }

    /**
     * 修改驾校表
     */
    @PutMapping
    @ApiOperation("修改驾校表")
    public Result update(@RequestBody Campus campus) {
        campus.setUpdateTime(new Date());
        boolean b = campusService.saveOrUpdate(campus);
        if (b) {
            return Result.ok("修改驾校表成功", campus);
        } else {
            return Result.failed("修改驾校表失败");
        }
    }

    /**
     * 删除驾校表
     */
    @DeleteMapping("{id}")
    @ApiOperation("删除驾校表")
    public Result delete(@PathVariable Long id) {
        boolean b = campusService.removeById(id);
        if (b) {
            return Result.ok("删除驾校表成功");
        } else {
            return Result.failed("删除驾校表失败");
        }
    }
}
